﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing;

namespace System
{
	public static partial class Math3DWin32
	{
		public static PointF ToPointF(this V3D P)
		{
			return new PointF((float)P.X, (float)P.Y);
		}
		public static void DrawCircle(this V3D C, Graphics G, Pen P, double R)
		{
			G.DrawEllipse(P, (float)(C.X - R), (float)(C.Y - R), (float)R * 2, (float)R * 2);
		}
		public static void DrawCircle(Graphics G, Pen P, V3D[] Pts)
		{
			Line3D A = new Line3D((Pts[0] + Pts[1]) / 2, (Pts[0] + Pts[1]) / 2 + ((Pts[1] - Pts[0]).RotZ(90)));
			Line3D B = new Line3D((Pts[1] + Pts[2]) / 2, (Pts[1] + Pts[2]) / 2 + ((Pts[2] - Pts[1]).RotZ(90)));
			V3D X = A * B;
			Double R = (Pts[0] - X).Length;
			X.DrawCircle(G, P, R);
		}
		public static void FillCircle(Graphics G, Brush B, V3D C, double R)
		{
			G.FillEllipse(B, (float)(C.X - R), (float)(C.Y - R), (float)R * 2, (float)R * 2);
		}
		public static void FillCircle(Graphics G, Brush Br, V3D[] Pts)
		{
			Line3D A = new Line3D((Pts[0] + Pts[1]) / 2, (Pts[0] + Pts[1]) / 2 + ((Pts[1] - Pts[0]).RotZ(90)));
			Line3D B = new Line3D((Pts[1] + Pts[2]) / 2, (Pts[1] + Pts[2]) / 2 + ((Pts[2] - Pts[1]).RotZ(90)));
			V3D X = A * B;
			Double R = (Pts[0] - X).Length;
			FillCircle(G, Br, X, R);
		}
		//public static void DrawArc(Graphics G,Pen P,V3D C,double R,double StartAngle,double Endangle)
		public static void DrawArc(Graphics G, Pen P, V3D[] Pts)
		{
			Line3D RA = new Line3D((Pts[0] + Pts[1]) / 2, (Pts[0] + Pts[1]) / 2 + ((Pts[1] - Pts[0]).RotZ(90)));
			Line3D RB = new Line3D((Pts[1] + Pts[2]) / 2, (Pts[1] + Pts[2]) / 2 + ((Pts[2] - Pts[1]).RotZ(90)));

			V3D X = RA * RB;
			Double R = (Pts[0] - X).Length;

			V3D A = Pts[0] - X;
			double startangle = V3D.Angle360D(V3D.Red, A, V3D.Blue);
			V3D B = Pts[1] - X;
			V3D C = Pts[2] - X;
			double endangle = V3D.Angle360D(A, C, V3D.Blue);
			if (V3D.Angle360D(A, B, V3D.Blue) > endangle)
				endangle -= 360;

			G.DrawArc(P, (float)(X.X - R), (float)(X.Y - R), (float)R * 2, (float)R * 2, -(float)startangle, -(float)endangle);
			//for (int i = 0; i < 3; i++)
			//	Pts[i].DrawGraphics(G, Brushes.Lime, 3);
		}
	}
}